using Gee;

namespace Atlantis
{
    public interface IEventAggregator : GLib.Object {
		public abstract void send_message<T>(T message);
		public abstract void add_listener(IEventHandler listener);
		public abstract void remove_listener(IEventHandler listener);
    }
    
    public interface IEventHandler : GLib.Object {
		public abstract bool handles_event<T>();
		public abstract void handle_event<T>(T event);
	}
	
	public class Event : GLib.Object {
		
	}
	
	public class OpenProjectEvent : Event {
		public ProjectNode Project { get; set; }
	}
    
    public class EventAggregator : GLib.Object, IEventAggregator, IInjectRegistry {
		
		public Registry Registry { get; set; }
		
		private ArrayList<IEventHandler> handlers;
		
		construct {
			handlers = new ArrayList<IEventHandler>();
		}	
		
		public void add_listener(IEventHandler listener){
			stderr.printf("in add_listener(IEventHandler listener)\n");
			handlers.add(listener);
		}
		
		public void remove_listener(IEventHandler listener){
			stderr.printf("in remove_listener(IEventHandler listener)\n");
			handlers.remove(listener);
		}
		
		public void send_message<T>(T message){
			stderr.printf("in send_message<T>(T message)\n");
			foreach(IEventHandler handler in handlers){
				if (handler.handles_event<T>()){
					handler.handle_event<T>(message);
				}
			}
		}
	}
}
